package com.three.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.three.entity.BranchGoods;
import com.three.entity.Outlibrary;
import com.three.entity.OutlibraryGoods;
import com.three.entity.Staff;
import com.three.mapper.BranchGoodsMapper;
import com.three.mapper.OutlibraryGoodsMapper;
import com.three.mapper.OutlibraryMapper;
import com.three.service.OutLibraryExService;
import com.three.vo.OutGoodsVo;
import com.three.vo.OutLibraryVo;
import com.three.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
public class OutLibraryExServiceImpl implements OutLibraryExService {
    @Autowired
    private OutlibraryMapper outlibraryMapper;
    @Autowired
    private OutlibraryGoodsMapper outlibraryGoodsMapper;
    @Autowired
    private BranchGoodsMapper branchGoodsMapper;
    @Autowired
    private HttpSession session;
    @Override
    public R getAllOutLibraryEx(Integer currentPage, Integer pageSize) {
        //开启分页
        PageHelper.startPage(currentPage,pageSize);
        //判断是否有权限看所有的店
        Staff staff = (Staff) session.getAttribute("staff");
        if (!staff.getBranchid().equals(12138)) {
            //查询该门店所有的出库单
            List<Outlibrary> outlibraries= outlibraryMapper.getAllOutLibraryExByBranchid(staff.getBranchid());
            //封装
            PageInfo<Outlibrary> pageInfo02 = new PageInfo<>(outlibraries);
            return R.ok(pageInfo02);
        }
        List<Outlibrary> list = outlibraryMapper.getAllOutLibraryEx();
        //封装
        PageInfo<Outlibrary> pageInfo = new PageInfo<>(list);
        return R.ok(pageInfo);
    }

    @Override
    public R getOutGoodsByCid(Integer cid,Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<OutlibraryGoods> outGoodsByCid = outlibraryGoodsMapper.getOutGoodsByCid(cid);
        PageInfo<OutlibraryGoods> pageInfo = new PageInfo<>(outGoodsByCid);
        return R.ok(pageInfo);
    }

    @Override
    public R pass(Integer cid) {
        Staff staff = (Staff) session.getAttribute("staff");
        //根据cid查询outlibrary表
        Outlibrary outlibrary = outlibraryMapper.selectByPrimaryKey(cid);
        //为该笔出库单设置审核状态和审核时间为当前时间
        outlibrary.setExamstaffid(staff.getStaffid());//审核人员
        outlibrary.setExamtime(new Date());//审核时间
        outlibrary.setExamstate(2);//审核状态
        //更改信息
        int i = outlibraryMapper.updateByPrimaryKeySelective(outlibrary);

        //完成审核通过后更改商品库存
        //根据cid 查询商品信息
        List<OutlibraryGoods> outGoodsByCidlist = outlibraryGoodsMapper.getOutGoodsByCid(cid);
        for (OutlibraryGoods outlibraryGoods : outGoodsByCidlist) {
            //根据goodsid和branchid查询商品数量
            Integer goodsid = outlibraryGoods.getGoodsid();
            Integer branchid = staff.getBranchid();
            Integer num = outlibraryGoods.getNum();
            BranchGoods branchGoods = branchGoodsMapper.selectByGidAndBid(goodsid,branchid);
            Integer counts = branchGoods.getCounts();
            //减去商品数量，设置出库后的商品数量
            Integer newCounts = counts-num;
            branchGoods.setCounts(newCounts);//商品数量
            int i1 = branchGoodsMapper.updateByPrimaryKeySelective(branchGoods);
        }
        return R.ok();
    }

    @Override
    public R reject(OutLibraryVo outLibraryVo) {
        Staff staff = (Staff) session.getAttribute("staff");
        //根据cid查询出库单
        Outlibrary outlibrary = outlibraryMapper.selectByPrimaryKey(outLibraryVo.getCid());
        //为该笔出库单设置审核状态和拒绝理由，以及审核时间
        outlibrary.setExamstaffid(staff.getStaffid());
        outlibrary.setExamstate(3);
        outlibrary.setRemarks(outLibraryVo.getRemarks());
        outlibrary.setExamtime(new Date());
        //更改信息
        int i = outlibraryMapper.updateByPrimaryKeySelective(outlibrary);
        if(i>0){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @Override
    public R yesOrNo() {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        List<Outlibrary> list = outlibraryMapper.getOutLibraryInformationByBranchid(branchid);
        //循环遍历list，看是否有未审核的出库单
        for (Outlibrary outlibrary : list) {
            Integer examstate = outlibrary.getExamstate();
            if(examstate==1){
                return R.error("有未完成出库审核的订单");
            }
        }
        return R.ok();
    }

    @Override
    public R getBranchNameByCid(Integer cid) {
        Outlibrary outlibrary = outlibraryMapper.selectByPrimaryKey(cid);
        Integer branchid = outlibrary.getBranchid();
        return R.ok(branchid);
    }
}
